<?php
namespace Hawk\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function __construct() {
        header('content-type:text/html;charset=utf-8');
        parent::__construct();
        if(!session('id')){
            echo "<script>top.location = '/index.php/Hawk/Login/login'</script>";exit;
        }
        //首页不需要权限验证
        if(CONTROLLER_NAME == 'Index'){
            return true;
        }
        if(CONTROLLER_NAME == 'Common'){
            return true;
        }
        if(session('id')==1){
            return true;
        }
        if(CONTROLLER_NAME== 'Index' && ACTION_NAME == 'edit'){
            return true;
        }
        $where = "module_name = '".MODULE_NAME."' and controller_name = '".CONTROLLER_NAME."' and action_name = '".ACTION_NAME."'";
        // echo $where;die;
        $admin = session('id');
        if($admin == 1){
            $sql = "select count(*) has from hawk_privilege where $where";
        }else{
            $sql = "SELECT count(*) has from  hawk_admin_role a LEFT JOIN hawk_role_privilege b
            on a.role_id = b.role_id LEFT JOIN hawk_privilege c 
            on b.pri_id = c.id where a.admin_id =$admin and $where";
        }
        $model = M();
        $has = $model->query($sql);
        if($has[0]['has']<1){
            if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){ 
                echo json_encode(array('code'=>1000,'msg'=>'你无此权限！'));
                exit;
            }else{
                hecho('你没有权限！<a onclick="history.go(-1)"></a>');
                exit;
            }
        }    
    }
    /**
     * 首页
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function index(){
        $this->display();
    }
    /**
     * 欢迎页
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function welcome(){
        
        $this->display();
    }
    /**
     * 个人修改密码
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function edit(){
        if(IS_POST)
        {
            $model = D('Hawk/Admin');
            // hecho(I('post.'));die;
            if($model->create(I('post.'), 2))
            {
                $user = $model->find(I('post.id'));
                if($model->save() !== FALSE)
                {
                    //写入操作日志
                    $content = "成功修改用户".$user['username'];
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
                    $this->success('修改成功！', U('welcome'));
                    exit;
                }else{
                    //写入操作日志
                    $content = "修改用户".$user['username']."失败";
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
                }
            }
            $this->error($model->getError());
        }
        $model = M('Admin');
        $data = $model->find(session('id'));
        $this->assign('data', $data);
        $this->display();
    }
    /**
     * 桌面解锁
     * @author hawk <2271030162@qq.com>
     * @return json
     */
    public function unlocked(){
        if (IS_POST) {
            if(hawk_get_password($_POST['password'])==session('staff_info')['password']){
                echo json_encode(array('code'=>1,'msg'=>'密码正确！'));
            }else{
                echo json_encode(array('code'=>0,'msg'=>'密码错误！'));
            }
        }
    }
    /**
     * 清理缓存
     * @author hawk <2271030162@qq.com>
     * @return mixed
     */
    public function clear(){
        if (Dir::delDir(RUNTIME_PATH) === false) {
            return $this->error('缓存清理失败！');
        }
        return $this->success('缓存清理成功！');
    }
    /**
     * 左部导航
     * @author hawk <2271030162@qq.com>
     * @return json
     */
    public function left(){
        $pmodel = M('');
        $user_id = session('id');
        if ($user_id==1) {
            $sql = "select * from hawk_privilege";
        }else{
            $sql = "select c.* from  hawk_admin_role a LEFT JOIN hawk_role_privilege b
            on a.role_id = b.role_id LEFT JOIN hawk_privilege c 
            on b.pri_id = c.id where a.admin_id =$user_id";
        }
        $parr = $pmodel->query($sql);
        $leftarr=array();
        foreach ($parr as $key => $value) {
            if ($value['parent_id'] == 0 ) {
                $val = array('text'=>$value['pri_name'],'icon'=>'&#xe620;');
                foreach ($parr as $k => $v) {

                    if ($v['parent_id']==$value['id']) {
                        $va = array('text'=>$v['pri_name'],'icon'=>'&#xe621;','href'=>'/index.php/'.$v['module_name'].'/'.$v['controller_name'].'/'.$v['action_name']); 
                        $val['subset'][]=$va;

                    }                    
                }
                $leftarr['data'][]=$val;
            }
        }
        $person = array('text'=>'个人信息管理','icon'=>'&#xe620;','subset'=>array(array('text'=> '密码修改', 'icon'=> '&#xe621;', 'href'=> '/index.php/Hawk/Index/edit')));
        if(empty($leftarr)){
            $leftarr['data'][0]=$person;
        }else{
            array_unshift($leftarr['data'],$person);
        }       
        echo json_encode($leftarr);
    }
    public function setPageBtn($title, $btnName, $btnLink){
        $this->assign('_page_title', $title);
        $this->assign('_page_btn_name', $btnName);
        $this->assign('_page_btn_link', $btnLink);
    }
}